package com.jintoufs.service.divided.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.divided.DividedMapper;
import com.jintoufs.dao.hotel.HotelOrderMapper;
import com.jintoufs.dao.scene.SceneOrderMapper;
import com.jintoufs.dao.tour.TourRouteOrderMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.divided.Divided;
import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.divided.DividedService;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.service.scene.SceneOrderService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.util.valid.ValidUtil;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangpeng on 2018/2/27.
 */
@Transactional
@Service("dividedService")
public class DividedServiceImpl implements DividedService{
    @Resource
    private CustomerService customerService;
    @Resource
    private DividedMapper dividedMapper;
    @Resource
    private ShopService shopService;
    @Resource
    private AccountDetailService accountDetailService;
    @Resource
    private SceneOrderService sceneOrderService;
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private TourRouteOrderService tourRouteOrderService;
    @Override
    public List<Map> getDivided(String customerId) {
        return dividedMapper.getDividedInfoByCustomerId(customerId);
    }

    @Resource
    private HotelOrderMapper hotelOrderMapper;
    @Resource
    private SceneOrderMapper sceneOrderMapper;
    @Resource
    private TourRouteOrderMapper tourRouteOrderMapper;

    @Override
    public Divided saveDivided(Divided divided) {
        dividedMapper.insertSelective(divided);
        return divided;
    }

    @Override
    public List<Map<String, Object>> getByFilter(QueryFilters filters_) {
        return dividedMapper.getByFilter(filters_);
    }

    @Override
    public void doDivided(Object orderObj,String id,String type) {
        switch (type){
            case Constants.ORDER_TYPE_HOTEL:{
                HotelOrder order = (HotelOrder)orderObj;
                AccountDetail accountDetail = new AccountDetail();
                Shop shop = shopService.selectByValueId(order.getHotelId());
                accountDetail.setShopId(shop.getId());
                accountDetail.setId(id);
                accountDetail.setAmountOfMoney(order.getSharePrice());
                accountDetail.setCreateDate(new Date());
                accountDetail.setOrderId(order.getId());
                accountDetail.setType(Constants.ACCOUNT_IN);
                accountDetail.setOrderType(Constants.ORDER_TYPE_HOTEL);
                accountDetailService.insert(accountDetail);
                order.setStatus(Constants.ORDER_STATUS_OVER);
                order.setReadStatus(Constants.ORDER_READ_STATUS_0);
                order.setModifyTime(new Date());
                hotelOrderService.updateOrder(order);

                //更新结算金额

                BigDecimal account = shop.getAccount();
                account=account.add(order.getSharePrice());
                shop.setAccount(account);
                shopService.save(shop,null);
                ///开始计算分润,
                customerService.computDilivid(order.getId(), order.getCustomerId(),
                        order.getPrice(),
                        order.getActPrice(),
                        order.getSharePrice(), null,
                        "0", order.getSpreadId());
                break;
            }
            case Constants.ORDER_TYPE_SCENE:{
                SceneOrder sceneOrder = (SceneOrder)orderObj;
                AccountDetail accountDetail = new AccountDetail();
                Map<String, Object> param = new HashMap<>();
                param.put("valueId", sceneOrder.getSceneId());

                Shop shop = shopService.selectByValueId(sceneOrder.getSceneId());
                accountDetail.setShopId(shop.getId());
                accountDetail.setId(id);
                accountDetail.setAmountOfMoney(sceneOrder.getSharePrice());
                accountDetail.setCreateDate(new Date());
                accountDetail.setOrderId(sceneOrder.getId());
                accountDetail.setType(Constants.ACCOUNT_IN);
                accountDetail.setOrderType(Constants.ORDER_TYPE_SCENE);
                accountDetailService.insert(accountDetail);
                sceneOrder.setStatus(Constants.ORDER_STATUS_OVER);
                sceneOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                sceneOrder.setModifyTime(new Date());
                sceneOrderService.updateOrder(sceneOrder);
                //更新结算金额

                BigDecimal account = shop.getAccount();
                account = account.add(sceneOrder.getSharePrice());
                shop.setAccount(account);
                shopService.save(shop,null);

                //计算分润

                customerService.computDilivid(sceneOrder.getId(), sceneOrder.getCustomerId(), sceneOrder.getPrice(),
                        sceneOrder.getActPrice(),
                        sceneOrder.getSharePrice(),
                        null, "0", sceneOrder.getSpreadId());
                break;
            }
            case Constants.ORDER_TYPE_TOUR:{
                TourRouteOrder tourRouteOrder = (TourRouteOrder)orderObj;
                AccountDetail accountDetail = new AccountDetail();

                Shop shop=shopService.selectByValueId(tourRouteOrder.getAgencyId());
                accountDetail.setShopId(shop.getId());
                accountDetail.setId(id);
                accountDetail.setAmountOfMoney(tourRouteOrder.getSharePrice());
                accountDetail.setCreateDate(new Date());
                accountDetail.setOrderId(tourRouteOrder.getId());
                accountDetail.setType(Constants.ACCOUNT_IN);
                accountDetail.setOrderType(Constants.ORDER_TYPE_TOUR);
                accountDetailService.insert(accountDetail);
                tourRouteOrder.setStatus(Constants.ORDER_STATUS_OVER);
                tourRouteOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
                tourRouteOrder.setModifyTime(new Date());
                tourRouteOrderService.update(tourRouteOrder);
                BigDecimal account = shop.getAccount();
                account=account.add(tourRouteOrder.getSharePrice());
                shop.setAccount(account);

                shopService.save(shop,null);

                //计算分润
                customerService.computDilivid(tourRouteOrder.getId(), tourRouteOrder.getCustomerId(), tourRouteOrder.getPrice(),
                        tourRouteOrder.getActPrice(),
                        tourRouteOrder.getSharePrice(),
                        null, tourRouteOrder.getNeedBill(), tourRouteOrder.getSpreadId());

                break;
            }
            default:{

            }
        }


    }

    @Override
    public List<Map> getShareOrders(Map param) {
        return  dividedMapper.getShareOrders(param);
    }

    @Override
    public AjaxMsg readOrder(String token, String ids,AjaxMsg ajaxMsg) {
        Customer customer =customerService.getByToken(token);
        Map param = new HashMap();
        param.put("ids", ids.split(","));
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("认证失败");
        }else{
            param.put("customerId", customer.getId());
            hotelOrderMapper.upDateOrderByStatusAndUser(param);
            sceneOrderMapper.upDateOrderByStatusAndUser(param);
            tourRouteOrderMapper.upDateOrderByStatusAndUser(param);
        }
        return ajaxMsg;
    }

    @Override
    public List<Map> getSharesByUser(String customerId) {
        return  dividedMapper.getShares(customerId);
    }
}
